﻿using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate;
using NHibernate.Linq;
using StructureMap;
using TimeTracker.Core.Domain;
using TimeTracker.Core.Domain.Entities;

namespace TimeTracker.Core.DataAccess.Implementation
{
    [Pluggable("Default")]
    public class OrganizacionaSemaRepository : RepositoryBase, IOrganizacionaSemaRepository
    {
        public List<OrganizacionaSema> UzmiSveOrganizacioneSeme()
        {
            var organizacioneSemeQuery = (from organizacioneSeme in NHibernateSession.Linq<OrganizacionaSema>()
                                          select organizacioneSeme).ToList();

            return organizacioneSemeQuery;
        }

        public void DodajOrganizacionuSemu(string imeOrganizacioneSeme)
        {
            var orgSema = new OrganizacionaSema
                                        {
                                            ImeOrganizacioneSeme = imeOrganizacioneSeme
                                        };

            NHibernateSession.SaveOrUpdate(orgSema);

            using (ITransaction transaction = NHibernateSession.BeginTransaction())
            {
                try
                {
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                }
            }

        }

        public void AzurirajOrganizacionuSemu(int orgSemaId, string novoImeOrganizacioneSeme)
        {
            var orgSema = (from organizacioneSeme in NHibernateSession.Linq<OrganizacionaSema>()
                           where organizacioneSeme.OrganizacionaSemaId == orgSemaId
                           select organizacioneSeme).SingleOrDefault();

            orgSema.ImeOrganizacioneSeme = novoImeOrganizacioneSeme;

            NHibernateSession.Update(orgSema);

            using (ITransaction transaction = NHibernateSession.BeginTransaction())
            {
                try
                {
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                }
            }

        }

        public void ObrisiOrganizacionuSemu(int organizacionaSemaId)
        {
            var orgSema = (from organizacioneSeme in NHibernateSession.Linq<OrganizacionaSema>()
                           where organizacioneSeme.OrganizacionaSemaId == organizacionaSemaId
                           select organizacioneSeme).SingleOrDefault();

            NHibernateSession.Delete(orgSema);

            using (ITransaction transaction = NHibernateSession.BeginTransaction())
            {
                try
                {
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                }
            }

        }
    }
}
